{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现以iris数据集为例，使用单层感知器学习算法，建立Sepal.Length、Sepal.Width、Petal.Length对Petal.Width回归问题的简单神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "44.610391883603896\n",
      "18.811901653735518\n",
      "10.120155007781296\n",
      "6.974210046982254\n",
      "5.756589264213023\n",
      "5.2360047749165535\n",
      "4.975454880273426\n",
      "4.816207611369656\n",
      "4.69954315364549\n",
      "4.603247692251133\n",
      "4.518610159646828\n",
      "4.441992215872995\n",
      "4.37170407177336\n",
      "4.306831036304336\n",
      "4.2467847407547445\n",
      "4.191127023758809\n",
      "4.139498143796355\n",
      "4.091585839213737\n",
      "4.047110899925092\n",
      "4.005819712785514\n",
      "3.9674799289396447\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# 准备基础数据\n",
    "iris = pd.read_csv(\"http://image.cador.cn/data/iris.csv\")\n",
    "x,y = iris.drop(columns=['Species','Petal.Width']),iris['Petal.Width']\n",
    "\n",
    "# 标准化处理\n",
    "x = x.apply(lambda v:(v-np.mean(v))/np.std(v))\n",
    "x = np.c_[[1]*x.shape[0],x]\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=1)\n",
    "\n",
    "# 初始化精度控制参数ε\n",
    "epsilon = 4.0\n",
    "\n",
    "# 初始化学习效率α\n",
    "alpha = 0.005\n",
    "\n",
    "# 精度控制变量d\n",
    "d = epsilon + 1\n",
    "\n",
    "# 用适当小的随机数初始化权向量 W\n",
    "w = np.random.uniform(0,1,4)\n",
    "\n",
    "while d >= epsilon:\n",
    "    d = 0\n",
    "    for i in range(x_train.shape[0]):\n",
    "        xi = x_train[i,:]\n",
    "        delta = np.sum(w*xi) - y_train.values[i]\n",
    "        w = w - alpha*delta*xi\n",
    "        d = d + delta**2\n",
    "        \n",
    "    print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看求出的权向量 ，并计算残差平方和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.19919401, -0.05710681,  0.06392559,  0.80475934])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.82217397715147"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum((y_test - np.sum(x_test*w,axis=1))**2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
